package com.yaoweigroup.community.adapter;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.exception.DbException;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.db.DBUtil;
import com.yaoweigroup.community.dialog.DialogCallBack;
import com.yaoweigroup.community.enity.HomeIcon;
import com.yaoweigroup.community.util.ApkDownloadUtil;
import com.yaoweigroup.community.util.DialogUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.Utils;

public class HomeGridAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<HomeIcon> items;
	private Context context;
	private DBUtil dbUtil;
	private int selectPosition = -1;
	private Vibrator vibrator;
	private long[] pattern = { 100, 50, 50, 100, 50 };
	private Handler handler = new Handler();

	public HomeGridAdapter(Context context, List<HomeIcon> items) {
		this.items = items;
		inflater = LayoutInflater.from(context);
		this.context = context;
		dbUtil = DBUtil.newInstance(context);
		vibrator = (Vibrator) context
				.getSystemService(Context.VIBRATOR_SERVICE);
		// 停止 开启 停止 开启

	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = this.inflater.inflate(R.layout.home_grid_item_view,
					null);
			holder.iv = (ImageView) convertView.findViewById(R.id.item_iv);
			holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
			holder.del_iv = (ImageView) convertView.findViewById(R.id.close_iv);
			convertView.setTag(holder);

			holder.iv.setOnLongClickListener(new OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {

					vibrator.vibrate(pattern, -1);
					if (position < 2) {
						Toast.makeText(context, "不可删除", 500).show();
					} else {
						LogUtil.d("wxf", "selectPosition=" + position);
						selectPosition = position;
						notifyDataSetChanged();
					}
					return false;
				}
			});
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.iv.setImageResource(items.get(position).getResId());
		holder.tv.setText(items.get(position).getTypeName());
		holder.del_iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					dbUtil.db.delete(items.get(position));
					refresh();
				} catch (DbException e) {
					LogUtil.e("wxf", e.toString());
				}
			}
		});

		if (selectPosition == position
				&& !items.get(position).getTypeName().equals("添加")) {
			holder.del_iv.setVisibility(View.VISIBLE);
		} else {
			holder.del_iv.setVisibility(View.GONE);
		}
		holder.iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					if (items.get(position).getTypeName().equals("实时公交")) {

						if (Utils.checkAPP(context, "com.yaowei.bus")) {
							Intent intent = new Intent();
							intent.setComponent(new ComponentName(
									"com.yaowei.bus", items.get(position)
											.getClassName()));
							intent.setAction(Intent.ACTION_VIEW);
							context.startActivity(intent);
						} else {
							DialogUtil.versionDialog(context,
									"你的手机未安装遥微实时公交APP", new DialogCallBack() {

										@Override
										public void viewClick() {
										}

										@Override
										public void ok() {
											try {
												ApkDownloadUtil util = new ApkDownloadUtil(
														context,
														handler,
														"http://www.yaoweigroup.com/bus.apk",
														"bus_apk");
												util.start();

											} catch (Exception e) {
											}
										}

										@Override
										public void init(Object obj) {

										}

										@Override
										public void cancel() {

										}
									}, "下载", "取消");
						}

					} else {
						Intent intent = new Intent(context, Class.forName(items
								.get(position).getClassName()));
						context.startActivity(intent);
					}

				} catch (Exception e) {
					if (items.get(position).getTypeName().equals("实时公交")) {
						DialogUtil.versionDialog(context, "你的手机未安装遥微实时公交APP",
								new DialogCallBack() {

									@Override
									public void viewClick() {
									}

									@Override
									public void ok() {
										try {
											ApkDownloadUtil util = new ApkDownloadUtil(
													context,
													handler,
													"http://www.yaoweigroup.com/bus.apk",
													"bus_apk");
											util.start();

										} catch (Exception e) {
										}
									}

									@Override
									public void init(Object obj) {

									}

									@Override
									public void cancel() {

									}
								}, "下载", "取消");
					}
					LogUtil.e("wxf", e.toString());
				}

			}
		});
		return convertView;
	}

	private class ViewHolder {
		ImageView iv;
		TextView tv;
		ImageView del_iv;
	}

	public void setDataChange(List<HomeIcon> list) {
		this.items = list;
		notifyDataSetChanged();
	}

	public void refresh() {
		try {
			this.selectPosition = -1;
			List<HomeIcon> list = dbUtil.db.findAll(HomeIcon.class);
			if (list != null && list.size() > 0) {
				if (list.size() == 9) {
					setDataChange(list);
				} else {
					HomeIcon gjhc = new HomeIcon();
					gjhc.setResId(R.drawable.add_icon);
					gjhc.setClassName("com.yaoweigroup.community.ui.home.AppsActivity");
					gjhc.setTypeName("添加");
					list.add(gjhc);
					setDataChange(list);
				}
			}
		} catch (Exception e) {

		}
	}
}
